package controllers

import (
	"Godzilla/common/request"
	"Godzilla/common/responses"
	"Godzilla/common/validate"
	"Godzilla/enum/code"
	"Godzilla/enum/fileds"
	"Godzilla/models"
	"Godzilla/repositories"
	"github.com/labstack/echo/v4"
	"net/http"
	"strconv"
)

func SaveRoleRouter(c echo.Context) error {
	var (
		roleId   	int64
		routerId    int64
	)

	r := new(models.RoleRouter)
	if err := c.Bind(r); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数有误", err))
	}

	roleId = r.RoleId
	routerId = r.RouterId

	if roleId == 0 || routerId == 0 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "角色跟路由id不能为0", ""))
	}


	roleRouterManager, err := repositories.NewRoleRouterManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	id, err := roleRouterManager.InsertRoleRouter(roleId, routerId)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", id))
}


func GetRoleRouter(c echo.Context) error {
	var (
		id  string
	)

	id = c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}

	roleRouterManager, err := repositories.NewRoleRouterManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	role, error := roleRouterManager.SelectRoleRouterById(id)
	if error != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", error.Error()))
	}
	return c.JSON(http.StatusOK, responses.Response(200, "成功", role))
}

func UpdateRoleRouter(c echo.Context) error {
	var (
		getId     string
		id		  int64
		status	  int8
		err		  error
	)

	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}
	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ParameterError, err.Error(), ""))
	}


	r := new(models.RoleRouter)
	if err := c.Bind(r); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	status = r.Status

	if !validate.StatusFileValidate(status) {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "status参数有误", ""))
	}

	roleRouterManager, err := repositories.NewRoleRouterManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	err = roleRouterManager.UpdateRoleRouterById(id, status)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func DeleteRoleRouter(c echo.Context) error {

	var (
		getId       string
		id 			int64
		err			error
	)
	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,"id不能为空", ""))
	}

	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,err.Error(), ""))
	}


	roleRouterManager, err := repositories.NewRoleRouterManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	_, err = roleRouterManager.DeleteRoleRouterById(id)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func GetRoleRouters(c echo.Context) error {
	var (
		page	  int64
		size  	  int64
		where     string
		sort	  string
		filed     string
	)

	p := new(request.Paging)
	if err := c.Bind(p); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	page = p.Page
	size = p.Size
	sort = p.Sort
	filed = p.SortFile

	if page == 0 {
		page = 1
	}
	if size == 0 {
		size = 10
	}
	if !validate.SortValidate(sort) {
		sort = fileds.ASC
	}
	if !validate.SortFileValidate(filed, &models.UserRole{}) {
		filed = fileds.ID
	}
	where = " status = 1 "

	roleRouterManager, err := repositories.NewRoleRouterManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	resultList , err :=  roleRouterManager.SelectRoleRouters(where, page, size, sort, filed)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", resultList))
}
